#include "StdAfx.h"
#include "Map.h"


Map::Map(Ogre::SceneManager* manager)
{
	this->mSceneMgr = manager;

	// Noeud de la map
	forestNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

	// Creation du sol
	Ogre::Plane plan(Ogre::Vector3::UNIT_Y, 0);
	Ogre::MeshManager::getSingleton().createPlane("sol", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plan, 1024, 1024, 1, 1, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
	Ogre::Entity *ground= mSceneMgr->createEntity("EntiteSol", "sol");
	ground->setMaterialName("Grass");

	// Creation de la foret
		Ogre::Vector2 PoissonDisc[] = {
        Ogre::Vector2(0.590838f, 0.965148f),
        Ogre::Vector2(0.137608f, 0.0498978f),
        Ogre::Vector2(0.432142f, 0.458022f),
        Ogre::Vector2(0.880917f, 0.0448927f),
        Ogre::Vector2(0.0515152f, 0.840205f),
        Ogre::Vector2(0.971068f, 0.603015f),
        Ogre::Vector2(0.029603f, 0.410108f),
        Ogre::Vector2(0.981353f, 0.99237f),
        Ogre::Vector2(0.397198f, 0.166173f),
        Ogre::Vector2(0.715323f, 0.308664f),
        Ogre::Vector2(0.33607f, 0.768242f),
        Ogre::Vector2(0.66689f, 0.662313f),
        Ogre::Vector2(0.64626f, 0.0763573f),
        Ogre::Vector2(0.950102f, 0.285806f),
        Ogre::Vector2(0.165868f, 0.587054f),
        Ogre::Vector2(0.790704f, 0.868374f),
        Ogre::Vector2(0.283944f, 0.977233f),
        Ogre::Vector2(0.217689f, 0.288156f),
        Ogre::Vector2(0.973632f, 0.82168f),
        Ogre::Vector2(0.323405f, 0.00210578f),
        Ogre::Vector2(0.813898f, 0.561357f),
        Ogre::Vector2(0.640675f, 0.474471f),
        Ogre::Vector2(0.460036f, 0.617389f),
        Ogre::Vector2(0.0102542f, 0.649312f),
        Ogre::Vector2(0.00979644f, 0.996307f),
        Ogre::Vector2(0.0283822f, 0.1742f),
        Ogre::Vector2(0.495926f, 0.775323f),
        Ogre::Vector2(0.536302f, 0.290231f),
        Ogre::Vector2(0.526078f, 0.0473647f),
        Ogre::Vector2(0.227393f, 0.857326f),
        Ogre::Vector2(0.792932f, 0.152898f),
        Ogre::Vector2(0.900266f, 0.703604f),
        Ogre::Vector2(0.838679f, 0.373699f),
        Ogre::Vector2(0.308512f, 0.630512f),
        Ogre::Vector2(0.278146f, 0.413953f),
        Ogre::Vector2(0.346477f, 0.282876f),
        Ogre::Vector2(0.450911f, 0.950835f),
        Ogre::Vector2(0.153691f, 0.959075f),
        Ogre::Vector2(0.801202f, 0.988342f),
        Ogre::Vector2(0.982269f, 0.142674f),
        Ogre::Vector2(0.629475f, 0.798639f),
        Ogre::Vector2(0.0110782f, 0.0541093f),
        Ogre::Vector2(0.958647f, 0.41908f),
        Ogre::Vector2(0.217139f, 0.714835f),
        Ogre::Vector2(0.759819f, 0.0161138f),
        Ogre::Vector2(0.246223f, 0.170995f),
        Ogre::Vector2(0.0936003f, 0.279153f),
        Ogre::Vector2(0.610126f, 0.191809f),
        Ogre::Vector2(0.758293f, 0.741691f),
        Ogre::Vector2(0.0440077f, 0.536882f),
        Ogre::Vector2(0.148656f, 0.361736f),
        Ogre::Vector2(0.548296f, 0.584979f),
        Ogre::Vector2(0.201209f, 0.465499f),
        Ogre::Vector2(0.253273f, 0.553148f),
        Ogre::Vector2(0.378887f, 0.531693f),
        Ogre::Vector2(0.411878f, 0.828608f),
        Ogre::Vector2(0.100742f, 0.697684f),
        Ogre::Vector2(0.893094f, 0.920713f),
        Ogre::Vector2(0.699362f, 0.954527f),
        Ogre::Vector2(0.617573f, 0.350383f),
        Ogre::Vector2(0.126316f, 0.147404f),
        Ogre::Vector2(0.399213f, 0.372509f),
        Ogre::Vector2(0.498215f, 0.18891f),
        Ogre::Vector2(0.415265f, 0.0314646f),
        Ogre::Vector2(0.861782f, 0.481979f),
        Ogre::Vector2(0.00454726f, 0.740379f),
        Ogre::Vector2(0.545427f, 0.428205f),
        Ogre::Vector2(0.00686666f, 0.302561f),
        Ogre::Vector2(0.752647f, 0.425062f),
        Ogre::Vector2(0.685476f, 0.567583f),
        Ogre::Vector2(0.828242f, 0.274606f),
        Ogre::Vector2(0.515366f, 0.885128f),
        Ogre::Vector2(0.364177f, 0.910367f),
        Ogre::Vector2(0.105075f, 0.461318f),
        Ogre::Vector2(0.988891f, 0.041139f),
        Ogre::Vector2(0.992706f, 0.693442f),
        Ogre::Vector2(0.323801f, 0.111454f),
        Ogre::Vector2(0.533097f, 0.678671f),
        Ogre::Vector2(0.150792f, 0.801599f),
        Ogre::Vector2(0.812738f, 0.663015f),
        Ogre::Vector2(0.702628f, 0.186682f),
        Ogre::Vector2(0.388043f, 0.68273f),
        Ogre::Vector2(0.863643f, 0.80752f),
        Ogre::Vector2(0.978088f, 0.506455f),
        Ogre::Vector2(0.636464f, 0.888394f),
        Ogre::Vector2(0.229713f, 0.0711386f),
        Ogre::Vector2(0.882321f, 0.16776f),
        Ogre::Vector2(0.43379f, 0.265023f),
        Ogre::Vector2(0.326518f, 0.364757f),
        Ogre::Vector2(0.0770592f, 0.620533f),
        Ogre::Vector2(0.879421f, 0.580035f),
        Ogre::Vector2(0.982788f, 0.889492f),
        Ogre::Vector2(0.939421f, 0.211859f),
        Ogre::Vector2(0.83816f, 0.728568f),
        Ogre::Vector2(0.385388f, 0.979034f),
        Ogre::Vector2(0.448836f, 0.714866f),
        Ogre::Vector2(0.709067f, 0.83871f),
        Ogre::Vector2(0.0660726f, 0.0937834f),
        Ogre::Vector2(0.925626f, 0.766747f),
        Ogre::Vector2(0.485061f, 0.384259f)};

	treeList = new std::vector<Tree*>;
	Ogre::Vector3 treePos;

	for (int i = 0; i < 100; i++)
	{
		treePos.x = (PoissonDisc[i][0] * 1000) - 450;
		treePos.y = 0;
		treePos.z = (PoissonDisc[i][1] * 1000) - 450;

		treeList->push_back(new Tree(i, manager, forestNode, treePos));
	}

	// On attache tous les objets au noeud
	forestNode->attachObject(ground);
}


Map::~Map(void)
{
	delete this->forestNode;
	delete this->mSceneMgr;
}

bool Map::CheckCollision(Ogre::Vector3 playerPos)
{
	for (int i = 0; i < treeList->size(); i++)
	{
		if (IsCollision(playerPos, 8, treeList->at(i)->position, 8))
		{
			return true;
		}
	}

	return false;
}

bool Map::IsCollision(Ogre::Vector3 p1, float r1, Ogre::Vector3 p2, float r2)
{
	float a,dx, dy;
	a = (r1 + r2) * (r1 + r2);
	dx = (float) (p1.x - p2.x);
	dy = (float) (p1.z - p2.z);

	if (a > (dx*dx) + (dy*dy))
	{
		return true;
	}
	return false;
}